package com.ms.p2p.service.impl;


import com.ms.p2p.bean.PageBean;
import com.ms.p2p.dao.RbacDao;
import com.ms.p2p.entity.system.RbacDept;
import com.ms.p2p.entity.system.RbacDeptRole;
import com.ms.p2p.entity.system.RbacRole;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RbacServiceImpl   {
    @Autowired
    private RbacDao rbacDao;


    public List findDepts() {
        return rbacDao.findDepts();
    }

    public void deleteByDeptAndRoles(String deptid, String[] role) {
        rbacDao.deleteByDeptId(deptid);
        RbacDeptRole rbacDeptRole = new RbacDeptRole();
        rbacDeptRole.setDept_id(Integer.parseInt(deptid));
        for (int i = 0; i < role.length; i++) {
            rbacDeptRole.setRole_id(Integer.parseInt(role[i]));
            rbacDao.insert(rbacDeptRole);
        }


    }


    public List<Map<String, Object>> getRoles(String deptid) {
        return rbacDao.getRoles(deptid);
    }


    @Cacheable(value = "system", key = "'MenuChilddept_id'+#dept_id+'pid'+#pid")
    public List<RbacRole> findDeptRolesByPid(int dept_id, String pid) {
        return  rbacDao.findDeptRolesByPid(dept_id,pid);
    }


    public List<HashMap<String,Object>> findPager(int begin, Integer pageSize) {
        return  rbacDao.findPager(begin,pageSize);
    }



    public int findCount() {
        return rbacDao.findCount();
    }

    public RbacDept selectDeptByid(String id) {
        return  rbacDao.selectDeptById(id);

    }


    public void saveOrUpdate(String id, String name) {
        if(StringUtils.isNotBlank(id)){
                rbacDao.updateRbacDept(id,name);
        }else{
            rbacDao.insertRbacDept(name);

        }

    }


    public boolean checkPremission(Integer id, String premission) {
       return  rbacDao.checkPremission(id,premission);
    }

    public List<Map<String, Object>> getPager(PageBean page) {
        return rbacDao.getPager(
                page.getBegin(), page.getPageSize());
    }
}
